package org.thingsboard.server.transport.mqtt.util;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import java.io.*;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

@Slf4j
public class RSACrypt {
    private static PrivateKey readPrivateKey = null;

    public static void main(String[] args) throws Exception {
        // 生成公钥和私钥
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(512);
        KeyPair pair = keyGen.generateKeyPair();
        PublicKey publicKey = pair.getPublic();
        PrivateKey privateKey = pair.getPrivate();
        try {
            FileOutputStream fosPub = new FileOutputStream("publicKey.bin");
            ObjectOutputStream oosPub = new ObjectOutputStream(fosPub);
            oosPub.writeObject(publicKey);

            FileOutputStream fosPri = new FileOutputStream("privateKey.bin");
            ObjectOutputStream oosPri = new ObjectOutputStream(fosPri);
            oosPri.writeObject(privateKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 公钥加密
        String username = "O8vaHvF58V";
        String pwd = "ivKaHEw9HZi4et4JLkpO";
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(pwd.getBytes());
        String encryptedString = Base64.getEncoder().encodeToString(encrypted);
        System.out.println("pwd Encrypted: " + encryptedString);

        byte[] usernameEncrypted = cipher.doFinal(username.getBytes());
        String usernameEncryptedString = Base64.getEncoder().encodeToString(usernameEncrypted);
        System.out.println("username Encrypted: " + usernameEncryptedString);
        // 私钥解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedString));
        String decryptedText = new String(decrypted);
        System.out.println("pwd Decrypted: " + decryptedText);

        byte[] userNamedecrypted = cipher.doFinal(Base64.getDecoder().decode(usernameEncryptedString));
        String usernamedecryptedText = new String(userNamedecrypted);
        System.out.println("username Decrypted: " + usernamedecryptedText);
    }

    public static String decrypt(String plainText){
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            if(readPrivateKey==null){
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("privateKey.bin"));
                readPrivateKey = (PrivateKey) ois.readObject();
                ois.close();
            }
            cipher.init(Cipher.DECRYPT_MODE, readPrivateKey);
            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(plainText));
            String decryptedText = new String(decrypted);
            System.out.println("Decrypted: " + decryptedText);
            return decryptedText;
        }catch (Exception e){
            log.error("Error in decrypting: " + e);
            return null;
        }
    }
}